// pages/login/login.js
import request from '../../utils/request'
/* 
  登录流程：
    1.收集表单数据
    2.前端验证
      用户信息是否合法：不合法则不发请求
      合法则带上表单数据发请求给后端
    3.后端验证
      用户是否存在：不存在则返回不存在
      密码是否正确：返回密码不正确
      都正确则返回数据
*/
Page({
  data: {
    phone: '',
    password: '',
    phoneErrorInfo: '',
    passwordErrorInfo: '',
    isPhoneLegal:false,
    isPasswordLegal:false
  },
  onLoad: function (options) {

  },
  handlePhone() {
    this.setData({
      isPhoneLegal:false
    })
    let { phone } = this.data;
    //手机号验证
    if (!phone) {
      this.setData({
        phoneErrorInfo:'手机号不能为空！'
      })
      return;
    }
    let phoneReg = /^1[3-9]\d{9}$/;
    if(!phoneReg.test(phone)){
      this.setData({
        phoneErrorInfo:'手机号格式不正确！'
      })
      return;
    }
    this.setData({
      phoneErrorInfo:'',
      isPhoneLegal:true
    })
  },
  handlePassword() {
    this.setData({
      isPasswordLegal:false
    })
    let { password } = this.data;
    //手机号验证
    if (!password) {
      this.setData({
        passwordErrorInfo:'密码不能为空！'
      })
      return;
    }
    /* let passwordReg = /^\w+{8,20}$/;
    if(!passwordReg.test(password)){
      this.setData({
        passwordErrorInfo:'密码必须为8-20位字母与数字的组合！'
      })
      return;
    } */
    this.setData({
      passwordErrorInfo:'',
      isPasswordLegal:true
    })
  },
  async login() {
    if(!(this.data.isPhoneLegal && this.data.isPasswordLegal)){
      wx.showToast({
        title: '输入格式错误',
        icon:'error'
      })
      return;
    }
    let {phone,password} = this.data
    let result = await request(`/login/cellphone`,{phone,password})
    if(result.code === 200){
      wx.setStorageSync('cookie', result.cookie);
      wx.setStorageSync('userInfo', JSON.stringify(result.profile))
      wx.reLaunch({
        url: '/pages/personal/personal',
      })
    }else{
      wx.showToast({
        title: `${result.message}`,
        icon: 'none'
      })
    }
  },
  async loginRandom(){
    wx.showToast({
      title: '登录成功',
    })
    wx.setStorageSync('cookie', "MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/wapi/feedback;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/weapi/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/wapi/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/api/clientlog;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/eapi/clientlog;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/api/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/neapi/clientlog;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/api/clientlog;;__remember_me=true; Max-Age=1296000; Expires=Fri, 19 Aug 2022 12:13:28 GMT; Path=/;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/eapi/feedback;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/wapi/clientlog;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/eapi/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/neapi/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/wapi/clientlog;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/api/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/openapi/clientlog;;__csrf=dbcf7d3874d5519dfca790a8fa6a3d06; Max-Age=1296010; Expires=Fri, 19 Aug 2022 12:13:38 GMT; Path=/;;NMTID=00OisDLA7aNgrjSL0fbnVyavSzMrp8AAAGCaMZj4w; Max-Age=315360000; Expires=Sun, 01 Aug 2032 12:13:28 GMT; Path=/;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/weapi/clientlog;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/weapi/clientlog;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/weapi/feedback;;MUSIC_U=c3f28f64d9af6893edce62b8d5942cdc8d1395613e2b05bde843cf1eff137508519e07624a9f00532c013a6b98b559737effa389fd15bac84d77ccecfe9739d20196c59eeb65fd7ba89fe7c55eac81f3; Max-Age=1296000; Expires=Fri, 19 Aug 2022 12:13:28 GMT; Path=/;;MUSIC_SNS=; Max-Age=0; Expires=Thu, 04 Aug 2022 12:13:28 GMT; Path=/;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/neapi/feedback;;MUSIC_R_T=1509169001376; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/eapi/clientlog;;MUSIC_A_T=1509168980451; Max-Age=2147483647; Expires=Tue, 22 Aug 2090 15:27:35 GMT; Path=/openapi/clientlog;")
    wx.setStorageSync('userInfo', JSON.stringify({
      "backgroundImgIdStr": "109951163641233144",
      "userId": 629782961,
      "userType": 0,
      "avatarImgIdStr": "109951166199051046",
      "vipType": 0,
      "nickname": "IMVemon",
      "birthday": -2209017600000,
      "gender": 1,
      "province": 610000,
      "city": 610100,
      "avatarImgId": 109951166199051040,
      "backgroundImgId": 109951163641233150,
      "accountStatus": 0,
      "avatarUrl": "https://p4.music.126.net/AmQSQKGFrVoIpyYXeDyNew==/109951166199051046.jpg",
      "defaultAvatar": false,
      "expertTags": null,
      "experts": {},
      "mutual": false,
      "remarkName": null,
      "authStatus": 0,
      "djStatus": 0,
      "followed": false,
      "backgroundUrl": "https://p4.music.126.net/D9X_OX3h7tfbo3N4ibz0Bg==/109951163641233144.jpg",
      "detailDescription": "",
      "description": "",
      "signature": "",
      "authority": 0,
      "avatarImgId_str": "109951166199051046",
      "followeds": 4,
      "follows": 18,
      "eventCount": 0,
      "avatarDetail": null,
      "playlistCount": 11,
      "playlistBeSubscribedCount": 0
    }))
    wx.reLaunch({
      url: '/pages/personal/personal',
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})